//
//  HMEveryDayListTabCell.m
//  HowToDo
//
//  Created by 胡坤 on 2017/1/6.
//  Copyright © 2017年 KY. All rights reserved.
//

#import "HMEveryDayListTabCell.h"
#import "HMListenAnyTimeResModel.h"
#import <objc/runtime.h>
@interface HMEveryDayListTabCell ()

@property(nonatomic,strong)UILabel* resourceNameLable;

@property(nonatomic,strong)UILabel * timeLable;

@property(nonatomic,strong)UIButton* selEditButton;

@property(nonatomic,strong)UIButton* progressBtn;
@end
@implementation HMEveryDayListTabCell

-(void)setUPUI
{
    //默认样式下右侧辅助功能按钮
    UIButton* btn = [[UIButton alloc]init];
    [btn setImage:[UIImage imageNamed:@"new_audio_more_btn"] forState:UIControlStateNormal];
    btn.bounds = CGRectMake(0, 0, 24, 24);
    [btn addTarget:self action:@selector(accessoryViewClickAction:) forControlEvents:UIControlEventTouchUpInside];
    self.accessoryView = btn;
    //显示资源名称的lable
    self.resourceNameLable = [[UILabel alloc]init];
    [self.contentView addSubview:self.resourceNameLable];
    [self.resourceNameLable mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.centerY.equalTo(self.contentView);
        make.left.equalTo(self.contentView).mas_offset(10);
        
        make.width.offset(self.bounds.size.width - CGRectGetMinX(self.accessoryView.frame));
    }];
    //显示下载进度和是否下载完成的按钮
    [self.contentView addSubview:self.progressBtn];
    [self.progressBtn sizeToFit];
    [self.progressBtn mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.left.equalTo(self.resourceNameLable);
        make.top.equalTo(self.resourceNameLable.mas_bottom).mas_offset(3);
    }];
    self.progressBtn.hidden = YES;
    //显示时间的lable
    self.timeLable = [[UILabel alloc]init];
    [self.contentView addSubview:self.timeLable];
    self.timeLable.textColor = [UIColor lightGrayColor];
    self.timeLable.font = [UIFont systemFontOfSize:12];
    CALayer *layer = [self.timeLable layer];
    layer.borderColor = [UIColor lightGrayColor].CGColor;
    layer.borderWidth = 1;
    [self.timeLable mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.top.equalTo(self.resourceNameLable.mas_bottom).mas_offset(3);
         make.left.equalTo(self.progressBtn.mas_right).mas_offset(2);
         make.bottom.equalTo(self.contentView).mas_offset(-10);
     }];
}

-(void)setFreeAudio:(HMListenAnyTimeResModel *)freeAudio
{
    _freeAudio = freeAudio;
    self.resourceNameLable.font = [UIFont systemFontOfSize:13];
    self.resourceNameLable.text = freeAudio.resource_name;
    self.timeLable.font = [UIFont systemFontOfSize:10];
    self.timeLable.text = freeAudio.hourTime;
}
-(void)accessoryViewClickAction:(UIButton*)sender
{
    self.clickAccessoryView(sender,self);
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self setUPUI];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setUPUI];
}
-(void)choseCurrentCell2DownLoad:(UIButton*)sender
{
    sender.selected = !sender.selected;
    self.selectFileToLoad(self, self.freeAudio, sender.selected);
}
#pragma mark --自定义编辑样式的方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    for (UIView *v1 in self.subviews)
    {
        if ([v1 isKindOfClass:[UIControl class]])
        {//此时获取到v1就是编辑按钮
             [v1 removeFromSuperview];
            if (v1.frame.origin.x < self.center.x)
            {
                self.selEditButton.frame = v1.frame;
                [self addSubview:self.selEditButton];
            }
        }
    }
}
//编辑介绍清除编辑按钮
-(void)willTransitionToState:(UITableViewCellStateMask)state
{
    if (state == UITableViewCellStateDefaultMask)
    {
        [self.selEditButton removeFromSuperview];
         self.selEditButton = nil;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }else if (state == UITableViewCellStateShowingEditControlMask)
    {
         self.selectionStyle = UITableViewCellSelectionStyleDefault;
    }
}

- (UIButton *)selEditButton
{
    if (!_selEditButton)
    {
        _selEditButton = [[UIButton alloc]init];
        [_selEditButton setImage:[UIImage imageNamed:@"audio_download_manager_none"] forState:UIControlStateNormal];
        [_selEditButton setImage:[UIImage imageNamed:@"audio_download_manager_all"] forState:UIControlStateSelected];
        [_selEditButton addTarget:self action:@selector(choseCurrentCell2DownLoad:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selEditButton;
}

-(void)setProgress:(float)progress
{
    _progress = progress;
    self.progressBtn.hidden = NO;
    self.progressBtn.selected = (progress ==1)?YES:NO;
    [self.progressBtn setTitle:[NSString stringWithFormat:@"%.2f%%",progress] forState:UIControlStateNormal];
    [self.progressBtn sizeToFit];
    if (progress == 1.0)
    {
        [self.progressBtn setTitle:@"" forState:UIControlStateNormal];
    }
}

- (UIButton *)progressBtn
{
    if (!_progressBtn)
    {
        _progressBtn = [[UIButton alloc]init];
        [_progressBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        _progressBtn.titleLabel.font = [UIFont systemFontOfSize:8];
        _progressBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_progressBtn setBackgroundImage:[UIImage imageNamed:@"new_audio_already_download"] forState:UIControlStateSelected];
        [_progressBtn setBackgroundImage:[UIImage new] forState:UIControlStateNormal];
    }
    return _progressBtn;
}
@end
